﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using Sita.MainModule.Services;
using Sita.Entities;
using Sita.Presentation.ViewModels;

namespace Sita.Presentation.Controllers
{
    public class WorkOrderController : DefaultController
    {
        WorkOrderService workOrderService;

        public WorkOrderController()
        {
            workOrderService = new WorkOrderService();
        }

        public ActionResult Index()
        {
            return View();
        }

        public PartialViewResult List(int? index, string sortColumn, bool? sortAscending, int? pageSize)
        {
            int TotalRows;

            int CurrentBusinessUnitID = this.AuthenticatedUser.Business[this.AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID;

            if (string.IsNullOrEmpty(sortColumn))
            {
                sortColumn = "Number";
            }

            sortAscending = sortAscending ?? true;

            index = index ?? 0;

            if (!pageSize.HasValue)
            {
                pageSize = 5;
            }

            ViewData["PageSizeFilter"] = new SelectList(new int[] { 5, 10, 15, 20 });

            //ViewData["HasEditPermission"] = this.AuthenticatedUser.CheckPermissionAvailavility("USREDT");

            return PartialView(
                    new GenericGridList<WorkOrder>(
                        workOrderService.GetPaginatedWorkOrdersByBussinesUnitID(CurrentBusinessUnitID, index.Value, pageSize.Value, sortColumn, sortAscending.Value, out TotalRows), index.Value, sortColumn, pageSize.Value, TotalRows, sortAscending.Value));
        }

        [HttpGet]
        public ActionResult View(int id)
        {
            WOModel WOModel = new WOModel();
            WOModel.WorkOrder = workOrderService.GetFullWO(id);
            WOModel.HandWorkPrice = float.Parse(WOModel.WorkOrder.WorkOrderEmployees.Sum(woe => woe.Price).ToString());
            WOModel.TotalPrice = float.Parse(WOModel.WorkOrder.WorkOrderReplacements.Sum(wor => wor.Price).ToString()) + WOModel.HandWorkPrice;
            
            return View(WOModel);
        }

        public ActionResult CreateOrUpdate(int? id)
        {
            BusinessUnitService BUService = new BusinessUnitService();
            WOModel WOModel = new WOModel();
            WOModel.Mode = id.HasValue ? WOModel.WOMode.Edit : WOModel.WOMode.Insert;

            if (WOModel.Mode == WOModel.WOMode.Edit)
            {
                WOModel.WorkOrder = workOrderService.GetFullWO(id.Value);
                WOModel.HandWorkPrice = float.Parse(WOModel.WorkOrder.WorkOrderEmployees.Sum(woe => woe.Price).ToString());
                WOModel.TotalPrice = float.Parse(WOModel.WorkOrder.WorkOrderReplacements.Sum(wor => wor.Price).ToString()) + WOModel.HandWorkPrice;

            }
            //Traigo la BU por si es un create y no esta dentro de WorkOrder.BusinessUnit
            WOModel.WorkOrderBU = BUService.GetBusinessUnitByID(this.AuthenticatedUser.Business[AuthenticatedUser.CurrentBusinessUnitIndex].BusinessUnitID);

            return View(WOModel);
        }


    }
}
